<script type="text/javascript">
$(function(){
	var pg = App.base.serverPicker.ui, pp = pg.params || {},
		pb = pg.children('.body'), hd = pb.children('.head'),
		bd = pb.children('.body'), gd = bd.children('.grid'),
		ft = pb.children('.foot'), slo = {};

	var frm = {
		env: hd.seek('env'),
		key: hd.seek('key'),
		find: hd.seek('find'),
		ok: ft.seek('ok'),
		close: ft.seek('close')
	};
	frm.env.mac('combo', {
		width: 80,
		boxTop: 34,
		boxWidth: 81,
		readOnly: 1,
		keyField: 'k',
		displayField: 'v',
		data: Cache.base.env(App.code)
	}).val(pp.env);

	gd.mac('grid', {
		key: 'id',
		cols : [{
			field: 'id', width: 24,
			title: pp.multi?'<input type="checkbox" name="all" />':'',
			render : function(r) {
				if (pp.multi) {
					return '<input type="checkbox" name="keys" value="' + r.id + '"/>';
				} else {
					return '<input type="radio" name="keys" value="' + r.id + '"/>';
				}
			}
		},{
			field: 'name', title : '服务器名', width: 220
		},{
			field: 'env', title : '环境', width: 60
		},{
			field: 'code', title : '服务器代码', width: 200
		},{
			field: 'ip', title : 'IP', width: 120
		},{
			field: 'level', title : '状态', width: 80,
			render: function(r){
				return Cache.base.server.status[r.status]
			}
		}],
		loader: {
			url: App.ctx + (pp.url || '/server/list.do'),
			params: {
				appId: pp.id,
				projId: pp.projId,
				env: pp.env,
				pageNo: 1,
				pageSize: 10
			},
			autoLoad: true
		},
		pagerLength: 10,
		afterLoad: function(dd, po, me){
			me.find('.tr').each(function(i, tr){
				$(tr).dblclick(function(){
					frm.ok.click();
				});
			});
			if (pp.multi) {
				me.seek('all').removeAttr('checked');
				me.seek('keys').each(function(i, n){
					var a = $(n), k = a.attr('value');
					if (slo[k]) {
						a.attr('checked', 'checked');
					} else {
						a.removeAttr('checked');
					}
				});
			}
		}
	});
	
// 	function adjust() {
// 		gd.parent().height(pg.height() - );
		gd.adjust();
// 	}

	gd.seek('all').click(function(){
		gd.seek('keys').prop('checked', $(this).prop('checked'));
	});
	frm.ok.click(function() {
		if (pp.multi) {
			$.each(gd.selected || [], function(i, a){
				var k = $(a).attr('name');
				slo[k] = gd.data[k];
			});
			pg.callback(slo);
		} else {
			var sl = gd.selected || [];
			if (sl.length) {
				var k = $(sl[0]).attr('name');
				pg.callback(gd.data[k]);
			}
		}
		pg.close();
	});

	function find(e){
		gd.load({ env: frm.env.val(), key: frm.key.val() });
	}
	frm.key.keydown(function(e) {
		if (e.keyCode == 13) {
			find(e);
		}
	});
	frm.find.click(find);
	frm.close.click(pg.close);
});
</script>
<div class="head">
	<span class="fl">环境：</span>
	<div class="combo fl" name="env"></div>
	<span>&nbsp;&nbsp;&nbsp;&nbsp;关键字：</span>
	<input name="key" placeholder="服务器编号、名称或IP地址" />
	<button name="find">查找</button>
</div>
<div class="body">
	<div class="grid"></div>
</div>
<div class="foot">
	<button name="ok">确定</button>
	&nbsp;&nbsp;&nbsp;&nbsp;
	<button name="close">取消</button>
</div>
